package com.cvv.controller;
import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvv.dao.*;
import com.cvv.entity.*;


/**
 * Created by Xander on 2018-06-14.
 */
@Controller
@RequestMapping("customer")
public class CustomerController {
	@Autowired
    private LcustomerMapper lcus;
	
	@Autowired
    private ProductMapper pm;
	
	@Autowired
    private LorderMapper lor;
	
	@Autowired
    private OrderMapper or;
	
	@RequestMapping("getLcustomerById/{customerid}")
	public String getLcustomerById(Model model,@PathVariable("customerid") int customerid){
		System.out.println("执行主键查询方法。。");
		Lcustomer cus=lcus.selectByPrimaryKey(customerid);
		model.addAttribute("cus",cus);
		System.out.println("客户信息:"+cus);
		return "cus_server/cus/update";
	}	
	
	@RequestMapping("getLorderById/{customerid}")
	public String getLorderById(Model model,@PathVariable("customerid") int customerid){
		System.out.println("执行主键查询订单方法。。");
		Lorder lorder=lor.selectLorderByLucsid(customerid);
		model.addAttribute("list",lorder);
		System.out.println("客户信息:"+lorder);
		return "cus_server/cus/orderformList";
	}	
	
	@RequestMapping("getAllLorder")
	public String getAllLorder(Model model){
		System.out.println("执行getAllLor()方法。。");
		List<Lorder> list=lor.selectAlltwo();
		System.out.println("客户信息:"+list);
		model.addAttribute("list", list);
		return "cus_server/cus/orderformList";
	}
	
	@RequestMapping("getAllLorders")
	public String getAllLorders(Model model){
		System.out.println("执行getAllLors()方法。。");
		List<Lorder> list=lor.selectAlltwo();
		System.out.println("客户信息:"+list);
		model.addAttribute("list", list);
		return "cus_server/order/search";
	}
	
	@RequestMapping("getAllLorderss")
	public String getAllLorderss(Model model){
		System.out.println("执行getAllLors()方法。。");
		List<Lorder> list=lor.selectAlltwo();
		System.out.println("客户信息:"+list);
		model.addAttribute("list", list);
		return "cus_server/order/deleteOrder";
	}
	
	@RequestMapping("getLcustomerInfoById/{customerid}")
	public String getLcustomerInfoById(Model model,@PathVariable("customerid") int customerid){
		System.out.println("执行详情查询方法。。");
		Lcustomer cus=lcus.selectByPrimaryKey(customerid);
		model.addAttribute("cus",cus);
		System.out.println("客户信息:"+cus);
		return "cus_server/cus/detail";
	}	
	
	@RequestMapping("updateLcustomerById")
	@ResponseBody
	public String updateLcustomerById(@RequestBody Lcustomer cus){
		System.out.println("执行主键修改方法。。"+cus);
		int num=lcus.updateByPrimaryKey(cus);
		System.out.println("客户信息:"+cus);
		if(num>0) {
			return "修改成功";
		}
		return "修改失败";
	}	
	
	@RequestMapping("getAllcustomer")
	public String getAllLcustomer(Model model){
		System.out.println("执行getAllUser()方法。。");
		List<Lcustomer> list=lcus.getAllLcustomer();
		System.out.println("客户信息:"+list);
		model.addAttribute("list", list);
		return "cus_server/cus/search";
	}
	
	@RequestMapping("getAllcusOther")
	public String getAllLcusOther(Model model){
		List<Lcustomer> list=lcus.getAllLcustomer();
		System.out.println("客户信息:"+list);
		model.addAttribute("list", list);
		return "cus_server/order/searchAdd";
	}
	
	@RequestMapping("getlCustomer")
	public String getLcustomerByif(Model model,String idcard,String mobile){
		System.out.println("执行多条件查询方法。。"+idcard+mobile);
		Lcustomer lcustomer = new Lcustomer();
		lcustomer.setIdcard(idcard);
		lcustomer.setMobile(mobile);
		List<Lcustomer> lc=lcus.getLcustomerByIf(lcustomer);
		for (Lcustomer lcustomer2 : lc) {
			System.out.println(lc);
		}
		model.addAttribute("list",lc);
		return "cus_server/order/searchAdd";
	}
	
	@RequestMapping("getlcustomer")
	public String getLcustomer(Model model,String idcard,String mobile){
		System.out.println("执行多条件查询方法。。"+idcard+mobile);
		Lcustomer lcustomer = new Lcustomer();
		lcustomer.setIdcard(idcard);
		lcustomer.setMobile(mobile);
		List<Lcustomer> lc=lcus.getLcustomerByIf(lcustomer);
		for (Lcustomer lcustomer2 : lc) {
			System.out.println(lc);
		}
		model.addAttribute("list",lc);
		return "cus_server/cus/search";
	}
	
	@RequestMapping(value="addCus",method=RequestMethod.POST)
	@ResponseBody
	public String addCustomer(@RequestBody Lcustomer cus){
		System.out.println("执行添加用户方法！");
		System.out.println(cus);
		int num=lcus.insertLcustomer(cus);
		if(num>0) {
			return "添加成功";
		}else {
			return "*号为必填，不能为空！";
		}
	}
	
	@RequestMapping("delcus/{customerid}")
	public String deleteUserById(@PathVariable("customerid") int customerid){
		System.out.println("执行deleteUserById()方法。。"+customerid);
		int num=lcus.deleteByPrimaryKey(customerid);
		return "redirect:../getAllcustomer"; 

	}	
	
	@RequestMapping("getAllfirstClass/{customerid}")
	public String getAllfirstClass(Model model,@PathVariable("customerid") int customerid) {
		System.out.println("执行。。。"+customerid);
		List<FirstClass> list = pm.getAllfirstclass();
		model.addAttribute("list", list);
		List<SecondClass> list2 = pm.getAllsecondclass();
		model.addAttribute("list2", list2);
		List<ProductName> list3 = pm.getAllproductname();
		model.addAttribute("list3", list3);
		model.addAttribute("customerid", customerid);
		return "cus_server/order/add";
	}
	
	@RequestMapping(value="addProduct",method=RequestMethod.POST)
	@ResponseBody
	public String addProduct(@RequestBody Product pro){
		System.out.println("执行添加商品方法！"+pro);
		int num = pm.insert(pro);
		if(num>0) {
			return "添加成功";
		}else {
			return "*号为必填，不能为空！";
		}
	}
	
	@RequestMapping("getAllproductById/{customerid}")
	public String getAllproductById(Model model,@PathVariable("customerid") int customerid) {
		System.out.println("执行。。。"+customerid);
		List<Product> list = pm.getAllproductById(customerid);
		model.addAttribute("list", list);
		model.addAttribute("customerid", customerid);
		return "cus_server/order/products";
	}
	
	@RequestMapping("deleteByPrimaryKey/{productid}/{customerid}")
	public String deleteByPrimaryKey(Model model,@PathVariable("productid") int productid,@PathVariable("customerid") int customerid) {
		System.out.println("执行。。。"+productid+customerid);
		pm.deleteByPrimaryKey(productid);
		return "redirect:../../getAllproductById/"+customerid;
	}
	
	@RequestMapping("getProductById/{productid}/{customerid}")
	public String getProductById(Model model,@PathVariable("productid") int productid,@PathVariable("customerid") int customerid) {
		System.out.println("执行。。。"+productid);
		Product pro=pm.getProductById(productid);
		model.addAttribute("pro", pro);
		model.addAttribute("customerid", customerid);
		return "cus_server/order/update";
	}
	
	@RequestMapping("getProId/{productid}")
	public String getProById(Model model,@PathVariable("productid") int productid) {
		System.out.println("执行。。。"+productid);
		Product pro=pm.getProductById(productid);
		model.addAttribute("pro", pro);
		return "cus_server/order/delProducts2";
	}
	
	@RequestMapping("getProductsById/{customer}")
	public String getProductsById(Model model,@PathVariable("customer") int customer) {
		System.out.println("执行。。。"+customer);
		List<Product> pro=pm.getAllproductById(customer);
		model.addAttribute("pro", pro);
		return "cus_server/order/delProcList";
	}
	
	@RequestMapping("updateProductById")
	@ResponseBody
	public String updateProductById(@RequestBody Product pro){
		System.out.println("执行主键修改商品方法。。"+pro);
		int num=pm.updateByPrimaryKey(pro);
		System.out.println("返回值："+num);
		if(num>0) {
			return "修改成功";
		}else {
		    return "修改失败";
		}
	}	
	
	@RequestMapping("updateProById")
	@ResponseBody
	public String updateProById(@RequestBody Product pro){
		System.out.println("执行主键修改订购数量方法。。"+pro);
		int num=pm.updateProductById(pro);
		System.out.println("返回值："+num);
		if(num>0) {
			return "修改成功";
		}else {
		    return "修改失败";
		}
	}	
	
	@RequestMapping(value="addOrder",method=RequestMethod.POST)
	@ResponseBody
	public String addOrder(@RequestBody Lorder lorder){
		System.out.println("执行添加Order方法！");
		System.out.println(lorder);
		int num=lor.insert(lorder);
		if(num>0) {
			return "添加成功";
		}else {
			return "*号为必填，不能为空！";
		}
	}
	
	@RequestMapping("selectByPrimaryKey/{orderid}")
	public String selectByPrimaryKey(Model model,@PathVariable("orderid") int orderid) {
		System.out.println("执行。。。"+orderid);
		Lorder lorder = lor.selectByPrimaryKey(orderid);
		model.addAttribute("lorder", lorder);
		return "cus_server/order/deleteOreder";
	}
	
	@RequestMapping("addUnbeproduct/{a}/{b}/{c}/{orderid}")
	public String addUnbeproduct(@PathVariable("a") String unbecause,@PathVariable("b") Date nubedate,@PathVariable("c") String operater,@PathVariable("orderid") int orderid) {
		Unbeproduct ubpt = new Unbeproduct();
		ubpt.setUnbecause(unbecause);
		ubpt.setNubedate(nubedate);
		ubpt.setOperater(operater);
		lor.addUnbeproduct(ubpt);
		lor.deleteByPrimaryKey(orderid);
		return "redirect:../../../../getAllLorders";
	}
	
	@RequestMapping(value="addPro",method=RequestMethod.POST)
	@ResponseBody
	public String addPro(@RequestBody Unbeproduct unbeproduct){
		System.out.println("执行添加用户方法！");
		System.out.println(unbeproduct);
		int num=lor.addUnbeproduct(unbeproduct);
		if(num>0) {
			return "成功退货";
		}else {
			return "退货失败";
		}
	}
}
